﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ICSharpCode.SharpZipLib.Zip;
using Aspose.Words;
using System.IO;
using System.Data;
using BySoft.Server.Common;
using Aspose.Words.Tables;
using BySoft.Server.Consts;
using BySoft.Server.Business;
using Aspose.Cells;
using BySoft.Web.Utility;
using BySoft.Server.Common.Enum;
using System.Runtime.InteropServices;

namespace BySoft.Web.Site.ComPage
{
    public class ClsExport
    {
        private static int iDelayTime = 600;
        public static void zipFile(string path, string destFileName)
        {
            try
            {
                FastZip fz = new FastZip();
                fz.CreateEmptyDirectories = true;
                fz.CreateZip(destFileName, path, true, "");
                fz = null;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public static void DownloadTransmit(HttpResponse Response, string strFileName)
        {
            FileInfo fi = new FileInfo(strFileName);
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fi.Name));
            Response.TransmitFile(strFileName);
        }

        public static void DownloadTransmit(HttpResponse Response, HttpServerUtility Server, string strFileName)
        {
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=z.zip");
            Response.TransmitFile(strFileName);
        }

        private static void ExportFile(HttpServerUtility Server, string strPath, DataTable dt, string strFileName, string strPerFile)
        {
            string tmppath = Server.MapPath(strFileName);
            Document doc = new Document(tmppath); //载入模板    
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            foreach (DataRow row in dt.Rows)
            {
                Document dstDoc = (Document)doc.Clone(true);
                foreach (DataColumn col in dt.Columns)
                {
                    string str = col.ColumnName;
                    if (dstDoc.Range.Bookmarks[str] != null)
                    {
                        Bookmark mark = dstDoc.Range.Bookmarks[str];
                        mark.Text = CommonMethod.GetString(row[str]);
                    }
                }
                dstDoc.Save(string.Format(strPath + "//{0}.doc", row[strPerFile].ToString().Replace(":", "_")));
            }
        }

        private static void ZipFile(string strPath, string strCompressFile)
        {
            zipFile(strPath, strCompressFile);
        }

        private static void TramfitFile(HttpResponse Response, HttpServerUtility Server, string strFileName)
        {
            DownloadTransmit(Response, Server, strFileName);
        }

        private static void DeleteDir(string strPath)
        {
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath, true);
            }
        }


        public static void ExportData(HttpResponse Response, HttpServerUtility Server, DataTable dtHead, string strFileName)
        {
            if (dtHead == null || dtHead.Rows.Count < 1)
            {
                return;
            }
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";
            switch (strFileName)
            {

                //case CommonConsts.g_strDocPurchase:
                //    //ExportDataInFirstCheckSuggest(Response, Server, strPath, dtHead, strFileName);
                //    break;

                default:
                    break;
            }

            if (dtHead.Rows.Count != 1)
            {
                ZipFile(strPath, strCompressFileName);
                //DeleteDir(strPath);
                TramfitFile(Response, Server, strCompressFileName);
            }
        }


        public static void ExportData2(HttpResponse Response, HttpServerUtility Server, DataTable dtHead, DataTable dtSel, string strFileName)
        {
            if (dtHead == null || dtHead.Rows.Count < 1)
            {
                return;
            }
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";
            switch (strFileName)
            {
                default:
                    break;
            }

            if (dtHead.Rows.Count != 1)
            {
                ZipFile(strPath, strCompressFileName);
                //DeleteDir(strPath);
                TramfitFile(Response, Server, strCompressFileName);
            }
        }


        public static void ExportData2(HttpResponse Response, HttpServerUtility Server, DataTable dtHead, string strFileName)
        {
            if (dtHead == null || dtHead.Rows.Count < 1)
            {
                return;
            }
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";
            switch (strFileName)
            {
                default:
                    break;
            }

            if (dtHead.Rows.Count != 1)
            {
                ZipFile(strPath, strCompressFileName);
                //DeleteDir(strPath);
                TramfitFile(Response, Server, strCompressFileName);
            }
        }



        //public static void ExportDataBody2(HttpResponse Response, HttpServerUtility Server, DataTable dtHead, DataTable dtBody, string strFileName)
        //{
        //    if (dtHead == null || dtHead.Rows.Count < 1)
        //    {
        //        return;
        //    }
        //    string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
        //    string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
        //    string strCompressFileName = strPath + ".zip";
        //    switch (strFileName)
        //    {
        //        default:
        //            break;
        //    }

        //    if (dtHead.Rows.Count != 1)
        //    {
        //        ZipFile(strPath, strCompressFileName);
        //        //DeleteDir(strPath);
        //        TramfitFile(Response, Server, strCompressFileName);
        //    }
        //}

        public static void ExportDataBody2(HttpResponse Response, HttpServerUtility Server, DataTable dt, string[] strs, string strFileName)
        {
            //if (dtProcessDetail == null || dtProcessDetail.Rows.Count < 1)
            //{
            //    return;
            //}
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";

            ExportDataOne(Response, Server, strPath, dt, strs, strFileName);

            if (dt.Rows.Count != 1)
            {
                ZipFile(strPath, strCompressFileName);
                //DeleteDir(strPath);
                TramfitFile(Response, Server, strCompressFileName);
            }
        }

        //private static void ExportDataSaleReturn(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtHead, DataTable dtBody, string strFileName)
        //{
        //    if (dtHead == null || dtHead.Rows.Count < 1)
        //    {
        //        return;
        //    }
        //    if (Directory.Exists(strPath))
        //    {
        //        Directory.Delete(strPath);
        //    }
        //    Directory.CreateDirectory(strPath);
        //    DataRow row = dtHead.Rows[0];

        //    Workbook excel = new Workbook();
        //    Worksheet worksheet = excel.Worksheets[0];

        //    DataTable dtProcessDetail = dtBody.Copy();
        //    dtProcessDetail.Columns.Add("NoGood");
        //    dtProcessDetail.Columns.Add("BillTypeName");
        //    int iTotalNum = 0;
        //    decimal dTotalAmount = 0;
        //    foreach (DataRow item in dtProcessDetail.Rows)
        //    {
        //        item["NoGood"] = row["NoGood"];
        //        item["BillTypeName"] = row["BillTypeName"];
        //    }
        //    dtProcessDetail.TableName = "A";
        //    WorkbookDesigner designer = new WorkbookDesigner();
        //    string path = Server.MapPath(strFileName);
        //    designer.Open(path);
        //    designer.SetDataSource(dtProcessDetail);
        //    designer.SetDataSource("CustomerName", "退货单位：" + CommonMethod.GetString(row["CustomerName"]));
        //    designer.SetDataSource("BillDate", "退货日期；" + CommonMethod.ConvertToDate(row["BillDate"]));
           

        //    designer.Process();

        //    string fileToSave = System.IO.Path.Combine(Server.MapPath("./"), "Files/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
        //    if (File.Exists(fileToSave))
        //    {
        //        File.Delete(fileToSave);
        //    }


        //    //designer.Save(fileToSave, FileFormatType.Excel2003);
        //    ////打开Excel文件
        //    //Process.Start(fileToSave);
        //    fileToSave = row["SaleOutGoodsID"].ToString() + ".xls";
        //    designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
        //    designer = null;
        //}

        //private static void ExportDataSaleOut(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtHead, DataTable dtBody, string strFileName)
        //{
        //    if (dtHead == null || dtHead.Rows.Count < 1)
        //    {
        //        return;
        //    }
        //    if (Directory.Exists(strPath))
        //    {
        //        Directory.Delete(strPath);
        //    }
        //    Directory.CreateDirectory(strPath);
        //    DataRow row = dtHead.Rows[0];

        //    Workbook excel = new Workbook();
        //    Worksheet worksheet = excel.Worksheets[0];

        //    DataTable dtProcessDetail = dtBody.Copy();
        //    dtProcessDetail.Columns.Add("Amount");
        //    int iTotalNum = 0;
        //    decimal dTotalAmount = 0;
        //    foreach (DataRow item in dtProcessDetail.Rows)
        //    {
        //        int iNum = CommonMethod.ConvertToInt(item["OutGoodsNum"]);//
        //        item["Amount"] = CommonMethod.ConvertToDecimal(item["Price"]) * iNum;
        //        iTotalNum += iNum;
        //        dTotalAmount += CommonMethod.ConvertToDecimal(item["Amount"]);
        //    }
        //    dtProcessDetail.TableName = "A";
        //    WorkbookDesigner designer = new WorkbookDesigner();
        //    string path = Server.MapPath(strFileName);
        //    designer.Open(path);
        //    designer.SetDataSource(dtProcessDetail);
        //    designer.SetDataSource("SendGoodID", CommonMethod.GetString(row["SaleOutGoodsID"]));
        //    designer.SetDataSource("SendDate", CommonMethod.ConvertToDate(row["BillDate"]));
        //    designer.SetDataSource("CustomerName", CommonMethod.GetString(row["CustomerName"]));
        //    designer.SetDataSource("Contactor", CommonMethod.GetString(row["Contactor"]));
        //    designer.SetDataSource("Tel", CommonMethod.GetTel(row["Tel1"], row["Tel2"]));
        //    designer.SetDataSource("SendAddress", CommonMethod.GetString(row["SendAddress"]));
        //    designer.SetDataSource("Agent", CommonMethod.GetString(row["Agent"]));
        //    designer.SetDataSource("DoBiller", CommonMethod.GetString(row["DoBiller"]));
        //    designer.SetDataSource("CarNum", CommonMethod.GetString(row["CarNum"]));
        //    designer.SetDataSource("TotalNum", iTotalNum);
        //    designer.SetDataSource("TotalAmount", dTotalAmount);

        //    designer.Process();

        //    string fileToSave = System.IO.Path.Combine(Server.MapPath("./"), "Files/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
        //    if (File.Exists(fileToSave))
        //    {
        //        File.Delete(fileToSave);
        //    }


        //    //designer.Save(fileToSave, FileFormatType.Excel2003);
        //    ////打开Excel文件
        //    //Process.Start(fileToSave);
        //    fileToSave = row["SaleOutGoodsID"].ToString() + ".xls";
        //    designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
        //    designer = null;
        //}

        //private static void ExportDataAllot(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtHead, DataTable dtBody, string strFileName)
        //{
        //    if (dtHead == null || dtHead.Rows.Count < 1)
        //    {
        //        return;
        //    }
        //    if (Directory.Exists(strPath))
        //    {
        //        Directory.Delete(strPath);
        //    }
        //    Directory.CreateDirectory(strPath);
        //    DataRow row = dtHead.Rows[0];

        //    Workbook excel = new Workbook();
        //    Worksheet worksheet = excel.Worksheets[0];
        //    int index = 1;
        //    int iReceiveNum = 0;
        //    decimal dPurValue = 0;

        //    DataTable dtProcessDetail = new DataTable();
        //    dtProcessDetail.Columns.Add("ProductName1");
        //    dtProcessDetail.Columns.Add("ProductName2");
        //    dtProcessDetail.Columns.Add("ProductName3");
        //    dtProcessDetail.Columns.Add("ProductName4");
        //    dtProcessDetail.Columns.Add("ProductName5");
        //    dtProcessDetail.Columns.Add("Num1");
        //    dtProcessDetail.Columns.Add("Num2");
        //    dtProcessDetail.Columns.Add("Num3");
        //    dtProcessDetail.Columns.Add("Num4");
        //    dtProcessDetail.Columns.Add("Num5");

        //    int c = 1;
        //    int c2 = 1;
        //    DataRow dr = null;
        //    foreach (DataRow item in dtBody.Rows)
        //    {
        //        if (c==1)
        //        {
        //            dr = dtProcessDetail.NewRow();
        //        }
        //        dr["ProductName" + c.ToString()] = item["ProductName"];
        //        dr["Num" + c.ToString()] = item["AllocateNum"];
        //        if (c == 5 || c2 == dtBody.Rows.Count)
        //        {
        //            c = 1;
        //            dtProcessDetail.Rows.Add(dr);
        //        }
        //        else
        //        {
        //            c++;
        //        }
        //        c2++;
        //    }
        //    dtProcessDetail.TableName = "A";
        //    WorkbookDesigner designer = new WorkbookDesigner();
        //    string path = Server.MapPath(strFileName);
        //    designer.Open(path);
        //    designer.SetDataSource(dtProcessDetail);
        //    designer.SetDataSource("CarNum", CommonMethod.GetString(row["CarNum"]));
        //    designer.SetDataSource("BillDate", CommonMethod.ConvertToDate(row["BillDate"]));
        //    designer.SetDataSource("Driver", CommonMethod.GetString(row["Driver"]));
        //    designer.SetDataSource("Agent", CommonMethod.GetString(row["Agent"]));
        //    designer.SetDataSource("InStockName", CommonMethod.GetString(row["InStockName"]));
        //    designer.SetDataSource("OutFactoryDate", CommonMethod.ConvertToDate(row["OutFactoryDate"]));
        //    designer.SetDataSource("ToFactoryDate", CommonMethod.ConvertToDate(row["ToFactoryDate"]));
        //    designer.SetDataSource("ToCustomerDate", CommonMethod.ConvertToDate(row["ToCustomerDate"]));
        //    designer.SetDataSource("OutCustomerDate", CommonMethod.ConvertToDate(row["OutCustomerDate"]));

        //    designer.Process();

        //    string fileToSave = System.IO.Path.Combine(Server.MapPath("./"), "Files/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
        //    if (File.Exists(fileToSave))
        //    {
        //        File.Delete(fileToSave);
        //    }


        //    //designer.Save(fileToSave, FileFormatType.Excel2003);
        //    ////打开Excel文件
        //    //Process.Start(fileToSave);
        //    fileToSave = row["AllocateID"].ToString() + ".xls";
        //    designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
        //    designer = null;
        //}

        //public static void ExportDataBody2PurRev(HttpResponse Response, HttpServerUtility Server, DataTable dtHead, DataTable dtBody, string strFileName,string strAgent)
        //{
        //    if (dtHead == null || dtHead.Rows.Count < 1)
        //    {
        //        return;
        //    }
        //    string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
        //    string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
        //    string strCompressFileName = strPath + ".zip";
        //    switch (strFileName)
        //    {
        //        default:
        //            break;
        //    }

        //    if (dtHead.Rows.Count != 1)
        //    {
        //        ZipFile(strPath, strCompressFileName);
        //        //DeleteDir(strPath);
        //        TramfitFile(Response, Server, strCompressFileName);
        //    }
        //}

        private static void DealDateItem(DataTable dt, string strColName)
        {
            if (dt.Columns.Contains(strColName))
            {
                dt.Columns.Add(strColName + "2");
                foreach (DataRow item in dt.Rows)
                {
                    item[strColName + "2"] = CommonMethod.ConvertToDate(item[strColName]);
                }
            }
        }

        private static void DealDate(DataTable dt)
        {
            DealDateItem(dt, "BillDate");
            DealDateItem(dt, "SaleOrderDate");
            DealDateItem(dt, "DeliveryDate");
        }

        private static void ExportDataOne(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dt, string[] strs, string strFileName)
        {
            //if (dtProcessDetail == null || dtProcessDetail.Rows.Count < 1)
            //{
            //    return;
            //}
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            DealDate(dt);

            Workbook excel = new Workbook();
            Worksheet worksheet = excel.Worksheets[0];
            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath(strFileName);
            designer.Open(path);
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.TableName = "A";
                designer.SetDataSource(dt);
            }
            if (strs != null)
            {
                for (int i = 0; i < strs.Length; i++)
                {
                    designer.SetDataSource("t" + i.ToString(), strs[i]);
                }
            }

            designer.Process();

            string fileToSave = GetFileName() + ".xls";
            designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            designer = null;
        }

        private static string GetFileName()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss");
        }

        private static void ExportDataPurReceive(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtHead, DataTable dtBody, string strFileName, string strAgent)
        {
            if (dtHead == null || dtHead.Rows.Count < 1)
            {
                return;
            }
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            DataRow row = dtHead.Rows[0];

            Workbook excel = new Workbook();
            Worksheet worksheet = excel.Worksheets[0];
            int index = 1;
            int iReceiveNum = 0;
            decimal dPurValue = 0;
            
            DataTable dt = dtBody.Copy();
            dt.Columns.Add("PurValue");
            foreach (DataRow item in dt.Rows)
            {
                if (string.IsNullOrEmpty(CommonMethod.GetString(item["MaterialVolume"])))
                {
                    item["PurValue"] = CommonMethod.ConvertToDecimal(item["ReceiveNum"]) * CommonMethod.ConvertToDecimal(item["Price"]);
                }
                else
                {
                    item["PurValue"] = CommonMethod.ConvertToDecimal(item["MaterialVolume"]) * CommonMethod.ConvertToDecimal(item["ReceiveNum"]) * CommonMethod.ConvertToDecimal(item["Price"]);
                }
                iReceiveNum += CommonMethod.ConvertToInt(item["ReceiveNum"]);
                dPurValue += CommonMethod.ConvertToDecimal(item["PurValue"]);
            }
            dt.TableName = "A";
            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath(strFileName);
            designer.Open(path);
            designer.SetDataSource(dt);
            designer.SetDataSource("PurReceiveID", row["PurReceiveID"].ToString());
            designer.SetDataSource("BillDate", CommonMethod.ConvertToDate(row["BillDate"]));
            designer.SetDataSource("TotalNum", iReceiveNum.ToString());
            designer.SetDataSource("TotalPurValue", dPurValue.ToString());
            designer.SetDataSource("Agent", strAgent);
            designer.SetDataSource("SupplierID", row["SupplierID"].ToString());

            designer.Process();

            string fileToSave = System.IO.Path.Combine(Server.MapPath("./"), "Files/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            if (File.Exists(fileToSave))
            {
                File.Delete(fileToSave);
            }


            //designer.Save(fileToSave, FileFormatType.Excel2003);
            ////打开Excel文件
            //Process.Start(fileToSave);
            fileToSave = row["PurReceiveID"].ToString() + ".xls";
            designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            designer = null;
        }

        private static void ExportDataPurReturn(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtHead, DataTable dtBody, string strFileName, string strAgent)
        {
            if (dtHead == null || dtHead.Rows.Count < 1)
            {
                return;
            }
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            DataRow row = dtHead.Rows[0];

            Workbook excel = new Workbook();
            Worksheet worksheet = excel.Worksheets[0];
            int index = 1;
            int iReceiveNum = 0;
            decimal dPurValue = 0;

            DataTable dt = dtBody.Copy();
            dt.Columns.Add("PurValue");
            foreach (DataRow item in dt.Rows)
            {
                if (string.IsNullOrEmpty(CommonMethod.GetString(item["MaterialVolume"])))
                {
                    item["PurValue"] = CommonMethod.ConvertToDecimal(item["ReceiveNum"]) * CommonMethod.ConvertToDecimal(item["Price"]);
                }
                else
                {
                    item["PurValue"] = CommonMethod.ConvertToDecimal(item["MaterialVolume"]) * CommonMethod.ConvertToDecimal(item["ReceiveNum"]) * CommonMethod.ConvertToDecimal(item["Price"]);
                }
                iReceiveNum += CommonMethod.ConvertToInt(item["ReceiveNum"]);
                dPurValue += CommonMethod.ConvertToDecimal(item["PurValue"]);
            }
            dt.TableName = "A";
            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath(strFileName);
            designer.Open(path);
            designer.SetDataSource(dt);
            designer.SetDataSource("PurReceiveID", row["PurReceiveID"].ToString());
            designer.SetDataSource("BillDate", CommonMethod.ConvertToDate(row["BillDate"]));
            designer.SetDataSource("TotalNum", iReceiveNum.ToString());
            designer.SetDataSource("TotalPurValue", dPurValue.ToString());
            designer.SetDataSource("Agent", strAgent);
            designer.SetDataSource("SupplierName", row["SupplierName"].ToString());

            designer.Process();

            string fileToSave = System.IO.Path.Combine(Server.MapPath("./"), "Files/" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");
            if (File.Exists(fileToSave))
            {
                File.Delete(fileToSave);
            }


            //designer.Save(fileToSave, FileFormatType.Excel2003);
            ////打开Excel文件
            //Process.Start(fileToSave);
            fileToSave = row["PurReceiveID"].ToString() + ".xls";
            designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            designer = null;
        }

        public static void ExportDataAndBody(HttpResponse Response, HttpServerUtility Server, DataTable dtHead, DataTable dtBody, string strFileName)
        {
            if (dtHead == null || dtHead.Rows.Count < 1)
            {
                return;
            }
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";
            switch (strFileName)
            {
                default:
                    break;
            }

            if (dtHead.Rows.Count != 1)
            {
                ZipFile(strPath, strCompressFileName);
                //DeleteDir(strPath);
                TramfitFile(Response, Server, strCompressFileName);
            }
        }

        public static void ExportDataBody2(HttpResponse Response, HttpServerUtility Server, DataTable dtProject, DataTable dtPerson, DataTable dtOther, string[] strs, string strFileName)
        {
            if (dtProject == null || dtProject.Rows.Count < 1)
            {
                return;
            }
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";

            ExportDataOne(Response, Server, strPath, dtProject, dtPerson, dtOther, strs, strFileName);

            //if (dtProcessDetail.Rows.Count != 1)
            //{
            //    ZipFile(strPath, strCompressFileName);
            //    //DeleteDir(strPath);
            //    TramfitFile(Response, Server, strCompressFileName);
            //}
        }

        private static void ExportDataOne(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtProject, DataTable dtPerson, DataTable dtOther, string[] strs, string strFileName)
        {
            //if (dtProject == null || dtProject.Rows.Count < 1)
            //{
            //    return;
            //}
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            //DealDate(dtProject);

            Workbook excel = new Workbook();
            Worksheet worksheet = excel.Worksheets[0];
            dtProject.TableName = "A";
            dtPerson.TableName = "B";
            dtPerson.TableName = "C";
            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath(strFileName);
            designer.Open(path);
            designer.SetDataSource(dtProject);
            designer.SetDataSource(dtPerson);
            designer.SetDataSource(dtOther);
            if (strs != null)
            {
                for (int i = 0; i < strs.Length; i++)
                {
                    designer.SetDataSource("t" + i.ToString(), strs[i]);
                }
            }

            designer.Process();

            string fileToSave = GetFileName() + ".xls";
            designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            designer = null;
        }

        public static void ExportDataBody2(HttpResponse Response, HttpServerUtility Server, DataTable dtProject, DataTable dtPerson, DataTable dtOther, DataTable dtPackage, string[] strs, string strFileName)
        {
            //if (dtProject == null || dtProject.Rows.Count < 1)
            //{
            //    return;
            //}
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";

            ExportDataOne(Response, Server, strPath, dtProject, dtPerson, dtOther, dtPackage, strs, strFileName);

            //if (dtProcessDetail.Rows.Count != 1)
            //{
            //    ZipFile(strPath, strCompressFileName);
            //    //DeleteDir(strPath);
            //    TramfitFile(Response, Server, strCompressFileName);
            //}
        }

        private static void ExportDataOne(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtProject, DataTable dtPerson, DataTable dtOther, DataTable dtPackage, string[] strs, string strFileName)
        {
            //if (dtProject == null || dtProject.Rows.Count < 1)
            //{
            //    return;
            //}
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            //DealDate(dtProject);
            AddTableEmpLine(dtProject);
            AddTableEmpLine(dtPerson);
            AddTableEmpLine(dtOther);
            AddTableEmpLine(dtPackage);

            Workbook excel = new Workbook();
            Worksheet worksheet = excel.Worksheets[0];
            dtProject.TableName = "A";
            dtPerson.TableName = "B";
            dtOther.TableName = "C";
            dtPackage.TableName = "D";
            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath(strFileName);
            designer.Open(path);
            designer.SetDataSource(dtProject);
            designer.SetDataSource(dtPerson);
            designer.SetDataSource(dtOther);
            designer.SetDataSource(dtPackage);
            if (strs != null)
            {
                for (int i = 0; i < strs.Length; i++)
                {
                    designer.SetDataSource("t" + i.ToString(), strs[i]);
                }
            }

            designer.Process();

            string fileToSave = GetFileName() + ".xls";
            designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            designer = null;
        }

        private static void AddTableEmpLine(DataTable dt)
        {
            DataRow row = dt.NewRow();
            dt.Rows.Add(row);
        }

        public static void ExportDataBody2(HttpResponse Response, HttpServerUtility Server, DataTable dtProject, DataTable dtPerson, DataTable dtOther, DataTable dtPackage, DataTable dtEquip, string[] strs, string strFileName)
        {
            //if (dtProject == null || dtProject.Rows.Count < 1)
            //{
            //    return;
            //}
            string strBasePath = Server.MapPath("~//ModuleWord//tmp//");
            string strPath = strBasePath + DateTime.Now.ToString("yyyyMMddHHmmss");
            string strCompressFileName = strPath + ".zip";

            ExportDataOne(Response, Server, strPath, dtProject, dtPerson, dtOther, dtPackage, dtEquip, strs, strFileName);

            //if (dtProcessDetail.Rows.Count != 1)
            //{
            //    ZipFile(strPath, strCompressFileName);
            //    //DeleteDir(strPath);
            //    TramfitFile(Response, Server, strCompressFileName);
            //}
        }

        private static void ExportDataOne(HttpResponse Response, HttpServerUtility Server, string strPath, DataTable dtProject, DataTable dtPerson, DataTable dtOther, DataTable dtPackage, DataTable dtEquip, string[] strs, string strFileName)
        {
            //if (dtProject == null || dtProject.Rows.Count < 1)
            //{
            //    return;
            //}
            if (Directory.Exists(strPath))
            {
                Directory.Delete(strPath);
            }
            Directory.CreateDirectory(strPath);
            //DealDate(dtProject);
            AddTableEmpLine(dtProject);
            AddTableEmpLine(dtPerson);
            AddTableEmpLine(dtOther);
            AddTableEmpLine(dtPackage);
            AddTableEmpLine(dtEquip);

            Workbook excel = new Workbook();
            Worksheet worksheet = excel.Worksheets[0];
            dtProject.TableName = "A";
            dtPerson.TableName = "B";
            dtOther.TableName = "C";
            dtPackage.TableName = "D";
            dtEquip.TableName = "E";

            WorkbookDesigner designer = new WorkbookDesigner();
            string path = Server.MapPath(strFileName);
            designer.Open(path);
            designer.SetDataSource(dtProject);
            designer.SetDataSource(dtPerson);
            designer.SetDataSource(dtOther);
            designer.SetDataSource(dtPackage);
            designer.SetDataSource(dtEquip);
            if (strs != null)
            {
                for (int i = 0; i < strs.Length; i++)
                {
                    designer.SetDataSource("t" + i.ToString(), strs[i]);
                }
            }

            designer.Process();

            string fileToSave = GetFileName() + ".xls";
            designer.Save(fileToSave, SaveType.OpenInExcel, FileFormatType.Excel2003, Response);
            designer = null;
        }
    }
}